home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / Dictionary.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  5KB  |  190 lines

  1. /* Dictionary.c -- implementation of Set of Associations
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     September, 1985
  21.  
  22. Function:
  23.     
  24. A Dictionary is a Set of Associations.  A Dictionary returns the value
  25. of an association given its key.
  26.  
  27. $Log:    Dictionary.c,v $
  28.  * Revision 3.0  90/05/20  00:19:26  kgorlen
  29.  * Release for 1st edition.
  30.  * 
  31. */
  32.  
  33. #include "Dictionary.h"
  34. #include "LookupKey.h"
  35. #include "Assoc.h"
  36.  
  37. #define    THIS    Dictionary
  38. #define    BASE    Set
  39. #define BASE_CLASSES BASE::desc()
  40. #define MEMBER_CLASSES
  41. #define VIRTUAL_BASE_CLASSES
  42.  
  43. DEFINE_CLASS(Dictionary,0,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Dictionary.c,v 3.0 90/05/20 00:19:26 kgorlen Rel $",NULL,NULL);
  44.  
  45. extern const int NIHCL_DUPKEY,NIHCL_KEYNOTFOUND;
  46.  
  47. Dictionary::Dictionary(unsigned size) : BASE(size) {}
  48.  
  49. void Dictionary::operator=(const Dictionary& d)
  50. {
  51.     this->Set::operator=(d);
  52. }
  53.  
  54. bool Dictionary::operator==(const Dictionary& d) const
  55. {
  56.     if (size() != d.size()) return NO;
  57.     DO(*this,LookupKey,a) if (!d.includesAssoc(*a)) return NO; OD
  58.     return YES;
  59. }
  60.  
  61. Object* Dictionary::add(Object& ob)
  62. {
  63.     assertArgClass(ob,*LookupKey::desc(),"add");
  64.     return Set::add(ob);
  65. }
  66.  
  67. Assoc* Dictionary::addAssoc(Object& key, Object& value)
  68. {
  69.     Assoc* a = new Assoc(key,value);
  70.     Assoc* b = Assoc::castdown(Set::add(*a));
  71.     if (a != b) {
  72.         delete a;
  73.         setError(NIHCL_DUPKEY,DEFAULT,this,className(),"addAssoc",key.className(),&key);
  74.     }
  75.     return b;
  76. }
  77.  
  78. Collection& Dictionary::addContentsTo(Collection& cltn) const
  79. {
  80.     DO(*this,LookupKey,a) cltn.add(*(a->value())); OD
  81.     return cltn;
  82. }
  83.  
  84. Object* Dictionary::remove(const Object& ob)
  85. {
  86.     assertArgClass(ob,*LookupKey::desc(),"remove");
  87.     return Set::remove(ob);
  88. }
  89.  
  90. Object* Dictionary::atKey(const Object& key) const
  91. {
  92.     register Object* p = findObjectWithKey(key);
  93.     if (p==nil) setError(NIHCL_KEYNOTFOUND,DEFAULT,this,className(),key.className(),&key);
  94.     else return LookupKey::castdown(p)->value();
  95. }
  96.  
  97. Object* Dictionary::atKey(const Object& key, Object& newValue)
  98. {
  99.     register Object* p = findObjectWithKey(key);
  100.     if (p==nil) setError(NIHCL_KEYNOTFOUND,DEFAULT,this,className(),key.className(),&key);
  101.     else return LookupKey::castdown(p)->value(newValue);
  102. }
  103.  
  104. LookupKey* Dictionary::assocAt(const Object& key) const
  105. {
  106.     Object* lk = findObjectWithKey(key);
  107.     if (lk == nil) return 0;
  108.     return LookupKey::castdown(lk);
  109. }
  110.  
  111. Collection& Dictionary::addKeysTo(Collection& cltn) const
  112. {
  113.     DO(*this,LookupKey,a) cltn.add(*a->key()); OD
  114.     return cltn;
  115. }
  116.  
  117. Collection& Dictionary::addValuesTo(Collection& cltn) const
  118. {
  119.     return addContentsTo(cltn);
  120. }
  121.  
  122. Object* Dictionary::keyAtValue(const Object& val) const
  123. {
  124.     DO(*this,LookupKey,a) if (val.isEqual(*a->value())) return a->key(); OD
  125.     return nil;
  126. }
  127.  
  128. unsigned Dictionary::occurrencesOf(const Object& val) const
  129. {
  130.     unsigned n =0;
  131.     DO(*this,LookupKey,a) if (val.isEqual(*a->value())) n++; OD
  132.     return n;
  133. }
  134.  
  135. bool Dictionary::includesAssoc(const LookupKey& asc) const
  136. {
  137.     register Object* p = findObjectWithKey(asc);
  138.     if (p==nil) return NO;
  139.     return asc.value()->isEqual(*(LookupKey::castdown(p)->value()));
  140. }
  141.  
  142. bool Dictionary::includesKey(const Object& key) const
  143. {
  144.     if (findObjectWithKey(key) == nil) return NO;
  145.     else return YES;
  146. }
  147.  
  148. bool Dictionary::isEqual(const Object& ob) const
  149. {
  150.     return ob.isSpecies(classDesc) && *this==castdown(ob);
  151. }
  152.  
  153. const Class* Dictionary::species() const { return &classDesc; }
  154.  
  155. LookupKey* Dictionary::removeAssoc(const LookupKey& asc)
  156. {
  157.     return LookupKey::castdown(remove(asc));
  158. }
  159.  
  160. LookupKey* Dictionary::removeKey(const Object& key)
  161. {
  162.     LookupKey* lk = assocAt(key);
  163.     if (lk == 0) setError(NIHCL_KEYNOTFOUND,DEFAULT,this,className(),key.className(),&key);
  164.     return removeAssoc(*lk);
  165. }
  166.  
  167. Dictionary::Dictionary(OIOifd& fd)
  168. :
  169. #ifdef MI
  170.     Object(fd),
  171. #endif
  172.     BASE(fd)
  173. {
  174. }
  175.  
  176. Dictionary::Dictionary(OIOin& strm)
  177. :
  178. #ifdef MI
  179.     Object(strm),
  180. #endif
  181.     BASE(strm)
  182. {
  183. }
  184.  
  185. int Dictionary::compare(const Object&) const
  186. {
  187.     shouldNotImplement("compare");
  188.     return 0;
  189. }
  190.